﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using System.Configuration; 

namespace websitev2.Controllers
{
    public class HomeController : Controller
    {

        public Models.StartStopEntities db = new Models.StartStopEntities(); 

        public ActionResult Index()
        {
            // Load the user. 

            var listoftrophies = StartStop.Helpers.Trophy.RecentTrophies(7); 

            ViewBag.RecentActivity = websitev2.Core.RecentActivity.ReturnRecentActivity(10); 

            var contents = (from content in db.SiteContents 
                            where content.ContentTag=="home" 
                            orderby content.CreatedOn descending
                            select content).Take(5) 
                            ;

            ViewBag.NewsContent = contents;

            var contents2 = (from content in db.SiteContents
                            where content.ContentTag == "involved"
                            orderby content.CreatedOn descending
                            select content).Take(4)
                            ;


            ViewBag.InvolvedCotent = contents2;

            ViewBag.Title = "Game stats, productivity stats and trophies | Startstop.me, all your stats in one place";
            ViewBag.Description = "All your stats in one place, from games, productivity, web sites and itunes and more";
            ViewBag.Keywords = "stats,statistics,trophies, achievements, social, friends";

            return View(listoftrophies);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Tour()
        {
            return View(); 
        }

        public ActionResult PageNotFound()
        {
            return View(); 
        }

        [HttpPost]
        public ActionResult About(websitev2.Models.Home.About oAbout)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    string _messageBody = oAbout.EmailAddress + System.Environment.NewLine + oAbout.Name + System.Environment.NewLine + oAbout.Message;
                    Core.Email.SendPlainEmail("startstop.me@gmail.com", "Contact form", _messageBody); 
                }
                catch
                {
                    // SMTP couldn't be found, so we should do something here to log the error. 
                }

                // Now we need to show a message to say that the form has been submitted. 
                // This should really be a partial render or something. 
                return Redirect("/Articles/News/ContactOk"); 

            }

            return View(oAbout); 
        }

       

    }
}
